package com.etc.traval.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.etc.traval.biz.DiaryBiz;
import com.etc.traval.biz.DiarycomBiz;
import com.etc.traval.biz.PlacecomBiz;
import com.etc.traval.biz.UserBiz;
import com.etc.traval.bizimpl.DiaryBizImpl;
import com.etc.traval.bizimpl.DiarycomBizImpl;
import com.etc.traval.bizimpl.PlacecomBizImpl;
import com.etc.traval.bizimpl.UserBizImpl;
import com.etc.travel.entity.Diary;
import com.etc.travel.entity.Place;
import com.etc.travel.entity.User;
import com.google.gson.Gson;
import com.google.gson.JsonObject;

public class DiaryServlet extends HttpServlet {

	/**
		 * Constructor of the object.
		 */
	public DiaryServlet() {
		super();
	}

	/**
		 * Destruction of the servlet. <br>
		 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
		 * The doGet method of the servlet. <br>
		 *
		 * This method is called when a form has its tag value method equals to get.
		 * 
		 * @param request the request send by the client to the server
		 * @param response the response send by the server to the client
		 * @throws ServletException if an error occurred
		 * @throws IOException if an error occurred
		 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		doPost(request, response);
	}

	/**
		 * The doPost method of the servlet. <br>
		 *
		 * This method is called when a form has its tag value method equals to post.
		 * 
		 * @param request the request send by the client to the server
		 * @param response the response send by the server to the client
		 * @throws ServletException if an error occurred
		 * @throws IOException if an error occurred
		 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		
		request.setCharacterEncoding("utf-8");
		//获取传入的flag
		DiaryBiz diaryBiz = new DiaryBizImpl();
		DiarycomBiz diarycomBiz = new DiarycomBizImpl();
		PlacecomBiz placecomBiz = new PlacecomBizImpl();
		
				String flag = null;
				if(request.getParameter("flag")!=null){
					flag = request.getParameter("flag");
				}
				if(flag.equals("alldiary")){
					
					int size=5;
				    int pageIndex=1;
				    //游记总数
				    int count=diaryBiz.queryDiaryCount();
				    int pageCount=(count%size==0)?(count/size):(count/size+1);		   
				    //获取热门景点数据
				    List<Place> list3 = placecomBiz.queryPlaceByCom(10000, 1);
				    
				    //获取排名前10 的游记
				    List<User> list2 = diarycomBiz.queryUserByCom(10, 1);
		
				   
				    //游记数量
				    int diaryNum=diaryBiz.queryDiaryCount();
				    //评论人数
				    int diarycomUserNum = diaryBiz.queryComUserNum();
				  
				    request.setAttribute("diaryNum", diaryNum);
				    request.setAttribute("diarycomUserNum", diarycomUserNum);
				    request.setAttribute("mylist3", list3);
				    request.setAttribute("mylist2", list2);
				    
					request.setAttribute("pageCount",pageCount);
					request.setAttribute("pageIndex",pageIndex);			
					request.getRequestDispatcher("diary.jsp").forward(request, response);
				}else if (flag.equals("getbegindiary")) {
					List<Diary> list = diarycomBiz.queryAllDiaryByCom(5, 1);
					//转换为Json
				    Gson gson=new Gson();
				    String jsonString= gson.toJson(list);
				    response.setContentType("text/html;charset=utf-8");
				    PrintWriter out = response.getWriter();
				    out.print(jsonString);
				    out.flush();
				    out.close(); 
				}else if (flag.equals("getmorehotdiary")) {
					int morehotornew =Integer.valueOf(request.getParameter("morehotornew"));
					int size=5*(morehotornew+1);
				    int pageIndex=1;
				  //获取分页游记
				    List<Diary> list = diarycomBiz.queryAllDiaryByCom(size, pageIndex);
				    //转换为Json
				    Gson gson=new Gson();
				    String jsonString= gson.toJson(list);
				    response.setContentType("text/html;charset=utf-8");;
				    PrintWriter out = response.getWriter();
				    out.print(jsonString);
				    out.flush();
				    out.close();    
				}else if (flag.equals("getmorenewdiary")) {
					int morehotornew =Integer.valueOf(request.getParameter("morehotornew"));
					int size=5*(morehotornew+1);
				  //获取分页游记
				    List<Diary> list = diaryBiz.queryDiary(size, 1);
				    //转换为Json
				    Gson gson=new Gson();
				    String jsonString= gson.toJson(list);
				    response.setContentType("text/html;charset=utf-8");;
				    PrintWriter out = response.getWriter();
				    out.print(jsonString);
				    out.flush();
				    out.close();
				    
				}else if(flag.equals("gethotdiary")){
					int size=5;
				    int pageIndex=1;
				  //获取分页游记
				    List<Diary> list = diarycomBiz.queryAllDiaryByCom(size, pageIndex);
				    //转换为Json
				    Gson gson=new Gson();
				    String jsonString= gson.toJson(list);
				    response.setContentType("text/html;charset=utf-8");;
				    PrintWriter out = response.getWriter();
				    out.print(jsonString);
				    out.flush();
				    out.close();  
				}else if (flag.equals("getnewdiary")) {
					int size=5;
				  //获取分页游记
				    List<Diary> list = diaryBiz.queryDiary(size, 1);
				    //转换为Json
				    Gson gson=new Gson();
				    String jsonString= gson.toJson(list);
				    response.setContentType("text/html;charset=utf-8");;
				    PrintWriter out = response.getWriter();
				    out.print(jsonString);
				    out.flush();
				    out.close(); 
				}else if (flag.equals("seachdiarybyplaceid")) {
						int size=5;
						int placeid = Integer.valueOf(request.getParameter("placeid"));
					    //获取分页游记
					    List<Diary> list = diaryBiz.queryDiaryByPlaceId(placeid, size, 1);
					    //转换为Json
					    Gson gson=new Gson();
					    String jsonString= gson.toJson(list);
					    response.setContentType("text/html;charset=utf-8");;
					    PrintWriter out = response.getWriter();
					    out.print(jsonString);
					    out.flush();
					    out.close(); 
				}else if (flag.equals("getmoreplacediary")) {
					int morehotornew =Integer.valueOf(request.getParameter("morehotornew"));
					int placeid = Integer.valueOf(request.getParameter("placeid"));
					int size=5*(morehotornew+1);
				  //获取分页游记
				    List<Diary> list = diaryBiz.queryDiaryByPlaceId(placeid, size, 1);
				    //转换为Json
				    Gson gson=new Gson();
				    String jsonString= gson.toJson(list);
				    response.setContentType("text/html;charset=utf-8");;
				    PrintWriter out = response.getWriter();
				    out.print(jsonString);
				    out.flush();
				    out.close();
				}else if (flag.equals("seachdiarybykey")) {
					int size=5;
				  //获取分页游记
				    String name=request.getParameter("placenamekey");
				    List<Diary> list = diaryBiz.queryDiaryByPlaceName(name, size, 1);
				    //转换为Json
				    Gson gson=new Gson();
				    String jsonString= gson.toJson(list);
				    response.setContentType("text/html;charset=utf-8");;
				    PrintWriter out = response.getWriter();
				    out.print(jsonString);
				    out.flush();
				    out.close();  
				}else if (flag.equals("getmoreseachdiary")) {
					String name=request.getParameter("placenamekey");
					int morehotornew =Integer.valueOf(request.getParameter("morehotornew"));
					int size=5*(morehotornew+1);
					List<Diary> list = diaryBiz.queryDiaryByPlaceName(name, size, 1);
					String placeflag = "inseachplacediary";
				    //转换为Json
				    Gson gson=new Gson();
				    String jsonString= gson.toJson(list);
				    response.setContentType("text/html;charset=utf-8");;
				    PrintWriter out = response.getWriter();
				    out.print(jsonString);
				    out.flush();
				    out.close();  
				}else if(flag.equals("toindex")){
					DiarycomBiz biz=new DiarycomBizImpl();
					List<Diary> list=biz.queryAllDiaryByCom(8, 1);
					for (Diary diary : list) {
						String str=diary.getDiary_content().replaceAll("</p><p>", "");
						if(str.length()<=100){
						}else{
						str=str.substring(0, 100)+"...";
						diary.setDiary_content(str);
						}
					}
					request.setAttribute("mylist", list);
					request.getRequestDispatcher("index.jsp").forward(request, response);
				}
				
				
				
				
				
	}

	/**
		 * Initialization of the servlet. <br>
		 *
		 * @throws ServletException if an error occurs
		 */
	public void init() throws ServletException {
		// Put your code here
	}

}
